﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Globalization;

//-- select * from sys.tables
//-- select * from ovnis
//-- insert into ovnis (tiempo, descr, lat, lon, vel) values (7, 'mingo', 11.232, 114.421, 0.00);
//-- select * from ovnis where descr LIKE '%ingo%'
//-- delete from ovnis where descr like 'mingo'
//-- select * from ovnis
//-- select * from sys.tables


public partial class dbadmin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnRun_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=w1701119_televoyeurdb;Integrated Security=SSPI;"))
        {
            try
            {
                conn.Open   ();
                SqlCommand  cmd = new SqlCommand(txtSQL.Text, conn);
                txtResultado.Text = cmd.ExecuteNonQuery().ToString();
                conn.Close  ();
            }
            catch (Exception exx)
            {
                txtResultado.Text = exx.Message;
            }
        }
    }

    protected void BtnRunScalar_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=w1701119_televoyeurdb;Integrated Security=SSPI;"))
        {
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(txtSQL.Text, conn);
                txtResultado.Text = cmd.ExecuteScalar().ToString();
                conn.Close();
            }
            catch (Exception exx)
            {
                txtResultado.Text = exx.Message;
            }
        }
    }

    protected void btnRunReader_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=w1701119_televoyeurdb;Integrated Security=SSPI;"))
        {
            try
            {
                conn.Open       ();
                SqlCommand      cmd     = new SqlCommand(txtSQL.Text, conn);
                SqlDataReader   sdr     = cmd.ExecuteReader();
                string          columnas= "";
                string          fila    = "";
                txtResultado.Text       = "";

                for (int i = 0; i < sdr.FieldCount; ++i)
                {
                    string separador = "";
                    if (i != sdr.FieldCount - 1)
                        separador = ", ";
                    else
                        separador = "";

                    columnas += sdr.GetName(i) + separador;
                }

                txtResultado.Text += columnas + "\r\n";

                while (sdr.Read())
                {
                    fila = "";

                    for (int i = 0; i < sdr.FieldCount; ++i)
                    {
                        string separador = "";
                        if (i != sdr.FieldCount - 1)
                            separador = ", ";
                        else
                            separador = "";

                        if (sdr.GetFieldType(i) == typeof(double))
                            fila += sdr.GetDouble(i).ToString(CultureInfo.InvariantCulture.NumberFormat) + separador;
                        else if (sdr.GetFieldType(i) == typeof(string))
                            fila += "'" + sdr.GetValue(i) + "'" + separador;
                        else
                            fila += sdr.GetValue(i) + separador;
                            //fila += sdr.GetFieldType(i) + separador;
                    }

                    txtResultado.Text += fila + "\r\n";
                }

                conn.Close();
            }
            catch (Exception exx)
            {
                txtResultado.Text = exx.Message;
            }
        }
    }
}
